#!/bin/bash

day_t=$(date -d'-1 day' +%Y-%m-%d)
day_t_1=$(date -d'-2 day' +%Y-%m-%d)

if [ $1 ]
then
  day_t=$1
  day_t_1=$(date -d"$1 -1 day" +%Y-%m-%d)
fi

echo "目标拉链表日期：$day_t , 上一日的拉链表是： $day_t_1 "

sql="
with lalian as (
SELECT
   *
FROM dwd.ums_member
where dt='${day_t_1}'
),
zengliang as (
SELECT
    *
FROM ods.ums_member
where dt='${day_t}'
)

INSERT INTO TABLE dwd.ums_member PARTITION(dt='${day_t}')
SELECT
    lalian.id
    ,lalian.member_level_id
    ,lalian.username
    ,lalian.password
    ,lalian.nickname
    ,lalian.phone
    ,lalian.status
    ,lalian.create_time
    ,lalian.icon
    ,lalian.gender
    ,lalian.birthday
    ,lalian.city
    ,lalian.job
    ,lalian.personalized_signature
    ,lalian.source_type
    ,lalian.integration
    ,lalian.growth
    ,lalian.luckey_count
    ,lalian.history_integration
    ,lalian.modify_time
    ,lalian.start_dt
    ,if(zengliang.id is not null, '${day_t_1}', lalian.end_dt )  as  end_dt
FROM lalian left join zengliang on lalian.id=zengliang.id and lalian.end_dt='9999-12-31'

UNION ALL

SELECT
    id
    ,member_level_id
    ,username
    ,password
    ,nickname
    ,phone
    ,status
    ,create_time
    ,icon
    ,gender
    ,birthday
    ,city
    ,job
    ,personalized_signature
    ,source_type
    ,integration
    ,growth
    ,luckey_count
    ,history_integration
    ,modify_time
    ,dt as start_dt
    ,'9999-12-31' as end_dt
FROM zengliang;
"


echo "'$sql'"

echo "-------------------------"

beeline -u jdbc:hive2://doitedu:10000 -n root  -e "\"$sql\""

if [ $? -eq 0 ]
then
  echo "增量数据拉链成功"
else
  echo "增量数据拉链失败"
fi
